<template>
  <div class="article">
    <div
      class="col center border"
      style="
        min-width: 800px;
        width: 70%;
        background: #fff;
        padding: 12px;
        margin: 12px;
      "
    >
      <div style="font-size: xx-large">{{ title }}</div>
      <div class="small-text bar">
        <span v-show="showTime">发布时间：{{ time }}</span>
        <span v-show="showAuthor === true">发布者：{{ author }}</span>
      </div>
      <div style="width: 80%">
        <Editor
          style="overflow-y: hidden"
          v-model="valueHtml"
          :defaultConfig="editorConfig"
          :mode="mode"
          @onCreated="handleCreated"
        />
        <div
          style="
            display: flex;
            flex-direction: column;
            align-items: start;
            margin-top: 16px;
          "
        >
          <el-link
            target="_blank"
            v-for="file in fileList"
            :href="file.url"
            :key="file.url"
          >
            {{ file.name }}
          </el-link>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import '@wangeditor/editor/dist/css/style.css' // 引入 css

import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue'
import { Editor } from '@wangeditor/editor-for-vue'
import axios from 'axios'
import { useRoute } from 'vue-router'
export default {
  components: { Editor },
  props: {
    showTime: {
      type: Boolean,
      default: true,
    },
    showAuthor: {
      type: Boolean,
      default: true,
    },
  },
  setup(props) {
    // 编辑器实例，必须用 shallowRef
    const editorRef = shallowRef()

    const title = ref('')
    const time = ref('')
    const author = ref('')
    const valueHtml = ref('')
    const fileList = ref([])
    const route = useRoute()

    onMounted(() => {
      const articleId = route.query.articleId
      console.log(route.query)
      getArticleById(articleId)
      console.log(props.showAuthor)
    })

    // 不配置工具栏，只能读
    const editorConfig = {}
    editorConfig.readOnly = true

    // 组件销毁时，也及时销毁编辑器
    onBeforeUnmount(() => {
      const editor = editorRef.value
      if (editor == null) return
      editor.destroy()
    })

    const handleCreated = (editor) => {
      editorRef.value = editor // 记录 editor 实例，重要！
    }

    // 根据文章id获取文章
    const getArticleById = (id) => {
      axios
        .get('/api/getEssayById', {
          params: {
            id: id,
          },
        })
        .then((res) => {
          console.log('id:' + id)
          let article = res.data.data
          console.log(article)
          title.value = article.title
          time.value = article.time
          author.value = article.author
          valueHtml.value = article.content
          // 文件列表要处理一下
          fileList.value = eval(JSON.parse(JSON.stringify(article.attachment)))
          console.log(time.value)
          console.log(author.value)
        })
    }

    return {
      editorRef,
      mode: 'default', // 或 'simple'
      editorConfig,
      handleCreated,
      getArticleById,
      title,
      time,
      author,
      valueHtml,
      fileList,
    }
  },
}
</script>

<style scoped>
.article {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.small-text {
  color: gray;
  font-size: small;
}
.bar {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  margin-top: 24px;
  width: 320px;
}
</style>
